/****************************************************************************/
/*                                                                          */
/*   FILE:    itc.h		                                                    */
/*   DATE:    25/06/97                                                      */
/*   PURPOSE: Header File Inter-Task Communication							*/
/*   TAB STOPS : 4															*/
/*   HISTORY:				                                    			*/
/*   Version    Date	  	    Written by	     Notes	    	    		*/
/*	 1.0	    25/06/1997      SCM              Initial implementation 	*/
/*                                                                    	    */
/*--------------------------------------------------------------------------*/
/*   GENERAL COMMENTS                                                 	    */
/*									    									*/
/****************************************************************************/
#ifndef __ITC_H
#define __ITC_H

#include "xmsg.h"
#include "sm_types.h"

#ifdef __cplusplus
extern "C" {
#endif
/*
#ifdef WIN32
	#include <time.h>
     struct timeval 
     {
        long tv_sec;
        long tv_usec;
     };
#else
	#include <sys/time.h>
#endif
*/
extern unsigned long	g_startTime;

#define ITC_INFINITE	  -1

// xpck	tags
#define XPACK_HEAD_SIZE	8
#define XPACK_DATA_SIZE	1024//256

#define XPCK_CIS		1
#define XPCK_LPDU 		2
#define XPCK_SPY 		3

#define DIRECTION_M2H	0
#define DIRECTION_H2M	1

#ifdef WIN32
typedef	struct
{
	unsigned char	m_type;
	unsigned char	m_direction;
	unsigned short	m_len;
	unsigned long	m_timestamp;
	unsigned char	m_buf[XPACK_DATA_SIZE];
}xpck_t;

#else
typedef	struct
{
	unsigned char	m_type;
	unsigned char	m_direction;
	unsigned long	m_timestamp;
	unsigned short	m_len;
	unsigned char	m_buf[XPACK_DATA_SIZE];
}__attribute__((packed))xpck_t;
#endif


#define MAX_APDU_LEN 1024
typedef struct
{
   char* m_data;
   int   m_length;
}DU;

int ITC_Init(unsigned char taskAid); //Initialize the ITC Module
int ITC_End(void); //End of the ITC Module

// Wait for a message, a timeout or a signal from the OS
BOOL ITC_RecvXmsg(xmsg_t* pXmsg, unsigned short timeout);
// Send a message to another Task
BOOL ITC_SendXmsg(const xmsg_t* pXmsg);

int itc_timer_thread(void* param);
unsigned short ITC_SetTimer(unsigned short timerId, unsigned long timeout_ms);
unsigned short ITC_SetTimerEx(unsigned short timerId, unsigned long timeout_ms, unsigned long param);
BOOL ITC_KillTimer(unsigned short timerId);

void* ITC_DynAlloc(unsigned long size);
void* ITC_DynReAlloc(void* ptr, unsigned long size);
int ITC_DynFree(void* ptr);

void ITC_Sleep(int delay_ms); //unit is milliseconds

void FreeXmsg(xmsg_t* pXmsg); //add by pmy

BOOL ITC_SendXpck(int fd, char type, char direction, char *buf, short len);

/****************************************************************************
*
* Specification of Abstract Syntax Notation One(ASN.1)
*
****************************************************************************/
void asnWriteInt16(unsigned char* pbBuf, unsigned short wValue);
unsigned short asnReadInt16(const unsigned char* pcbBuf);

void asnWriteInt32(unsigned char* pbBuf, unsigned long dwValue);
unsigned long asnReadInt32(const unsigned char* pcbBuf);

void asnWriteInt24(unsigned char* pbBuf, unsigned long dwValue);
unsigned long asnReadInt24(const unsigned char* pcbBuf);

unsigned char asnWriteLengthField(unsigned char* pbBuf, unsigned long dwLengthFieldValue);
unsigned char asnReadLengthField(const unsigned char* pcbBuf, unsigned long dwBufSize,
						 unsigned long* pdwLengthFieldValue);

#ifdef __cplusplus
}
#endif

#endif	// !__ITC_H
